package com.example.user.controller;

import com.example.core.annotation.HasPerm;
import com.example.model.response.Result;
import com.example.model.user.Role;
import com.example.model.user.User;
import com.example.user.service.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping("/{roleId}")
    public Result get(@PathVariable("roleId") Long roleId) {
        return Result.success(roleService.get(roleId));
    }

    @HasPerm("system:role:list")
    @GetMapping("/list")
    public Result list(Role role) {
        return Result.success(roleService.list(role));
    }

    @HasPerm("system:role:list")
    @GetMapping("/page")
    public Result page(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize, Role role) {
        PageHelper.startPage(pageNum, pageSize);
        List<Role> list = roleService.list(role);
        PageInfo<Role> pageInfo = new PageInfo<>(list);
        return Result.success(pageInfo);
    }


    @PostMapping
    public Result insert(@RequestBody Role role) {
        return Result.success(roleService.insert(role));
    }

    @PutMapping
    public Result update(@RequestBody Role role) {
        return Result.success(roleService.update(role));
    }

    @DeleteMapping("/{roleId}")
    public Result delete(@PathVariable("roleId") Long roleId) {
        return Result.success(roleService.delete(roleId));
    }


}
